using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using EntityFramework;

namespace Website.Pages
{
    public partial class MainT : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dtAllZone = BO.ProductUserBO.ProductUBO.GetListZoneByParentIDBO("0");
            rptList.DataSource = dtAllZone;
            rptList.DataBind();
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;
                string id = Convert.ToString(drv.Row["Zoneid"]);

                //var id = ((ZoneProduct)e.Item.DataItem).ZoneId;

                if (!string.IsNullOrEmpty(drv.Row["Zoneid"].ToString()))
                {
                    //string companiesIds = Convert.ToString(drv.Row["CompanyID"]).TrimEnd(',');
                    List<Company> lst = CompaniesBO.GetCompaniesByZoneIds(int.Parse(id));
                    if (lst.Count > 0)
                    {
                        var rptCompanies = (Repeater)e.Item.FindControl("rptCompanies");
                        if (rptCompanies != null)
                        {
                            rptCompanies.DataSource = null;
                            rptCompanies.DataSource = lst;
                            rptCompanies.DataBind();
                            rptCompanies.Visible = true;
                        }
                    }
                }
            }
        }

        protected void rptCompanies_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var CompanyId = ((Company)e.Item.DataItem).CompanyID;
                var ZoneIDCha = ((Company)e.Item.DataItem).ZoneId;
                //List<ZoneProduct> lst = ZoneProductBO.GetListZoneProductByCompanyId(int.Parse(id.ToString()));
                //DataRowView drv = (DataRowView)e.Item.DataItem;
                //string CompanyId = Convert.ToString(drv.Row["CompanyID"]);
                DataTable lst = BO.ProductUserBO.ProductUBO.GetZoneByCompanyID(CompanyId.ToString());
                if (lst.Rows.Count > 0)
                {
                    DataTable FilterZoneID = new DataTable();
                    FilterZoneID.Columns.Add("ZoneIdOther");
                    FilterZoneID.Columns.Add("CompanyID");
                    FilterZoneID.Columns.Add("title");
                    FilterZoneID.Columns.Add("ZoneID");
                    for (int i = 0; i < lst.Rows.Count; i++)
                    {
                        string ZoneIDDt = lst.Rows[i]["ZoneID"].ToString().ToLower().Trim();
                        string ZoneIDChaF = ZoneIDCha.ToString().ToLower().Trim();
                        if (ZoneIDDt.Equals(ZoneIDChaF))
                        {
                            DataRow dr = FilterZoneID.NewRow();
                            dr["ZoneIdOther"] = lst.Rows[i]["ZoneIdOther"].ToString();
                            dr["CompanyID"] = lst.Rows[i]["CompanyID"].ToString();
                            dr["title"] = lst.Rows[i]["title"].ToString();
                            dr["ZoneID"] = lst.Rows[i]["ZoneID"].ToString();
                            FilterZoneID.Rows.Add(dr);
                        }

                    }

                    var rptListChild = (Repeater)e.Item.FindControl("rptListChild");
                    if (rptListChild != null)
                    {
                        rptListChild.DataSource = null;
                        rptListChild.DataSource = FilterZoneID;
                        rptListChild.DataBind();
                        rptListChild.Visible = true;
                    }
                }
            }
        }

        protected void rptListChild_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            //{
            //    var title = ((ZoneProduct)e.Item.DataItem).title;
            //    var
            //    Literal ltrLink = (Literal)e.Item.FindControl("ltrLink");
            //    ltrLink.Text = string.Format("<a href="/<%# Eval("url") %>.htm" title="<%# HttpUtility.HtmlEncode(Eval("title")) %>">") 
            //}
        }
    }
}
